﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using ReflectionStudio.Core.Database;
using DSTVV06;
using ReflectionStudio.Core.FTPClient;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for DSTVV06.xaml
    /// </summary>
    public partial class DSTVV06 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DSTVV06Data m_Data, m_DataX;
        public SETVV setUI
        {
            get
            {
                return (SETVV)this.TryFindResource("SETUI");
            }
        }
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        DSTVV06Report m_rpt;
        ArrayList listVV06, ListColor, listBatchNo;
        #endregion
        public DSTVV06()
        {
            InitializeComponent();
            this.Name = "发货单";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new DSTVV06Data(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            SetUpFtp();
            btnLogFTP_Click(null, null);
            /*
            txtVKPAUFKRS.Text = "Z ";
            txtVKPAUFLFD.Text = "002030";
             * */

        }
        public void btnLogFTP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ftpsite = txtFTPSite.Text.Trim();
                string ftpport = txtFTPPort.Text.Trim();
                string ftpuser = txtFTPUser.Text.Trim();
                CheckFtp(ftpsite, ftpport, ftpuser, txtFTPPwd, IsBelongs2Admin.IsChecked == true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show("FTP登录信息有误。请联系你们的破管理员。");
            }
            finally
            {
                ftpClient.Close();
            }
        }
        private void ReloadVVTLIEF(DataRow dwVKPOS)
        {
            setUI.VVTLIEF.Clear();
           DataSet setResult = m_Data.GetVVTLIEF(dwVKPOS, false);
           setUI.Merge(setResult);
            foreach (DataRow dwTemp in setResult.Tables[0].Rows)
            {
                setUI.Merge(m_Data.GetVVLIEF(dwTemp, "", false));
            } 
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                m_rpt = new DSTVV06Report(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = PrintType;
                m_rpt.ftpClient = ftpClient;
                if (m_Data.exAgent._app == null)
                {
                    m_Data.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt.proxy = this.proxy;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.WriteExcelReport("EXCEL");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }

        private void btnGetVVLIEF_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string vvliefsch = m_Data.Right("00000000" + this.txtVVLIEFSCH.Text, 8);
                if (vvliefsch != "00000000")
                {
                    LoadData(vvliefsch);
                    return;
                }
                setUI.Clear();
                string krs = m_Data.Left(txtVKPAUFKRS.Text.ToString().Trim() + "  ", 2);
                string lfd = m_Data.Right("000000" + txtVKPAUFLFD.Text.ToString(), 6);
                setUI.Merge(m_Data.LoadVKPOS(krs, lfd));

                dwVKPOS = setUI.VKPOS.NewVKPOSRow();
                dwVKPOS["VKPAUFKRS"] = m_Data.Left(txtVKPAUFKRS.Text.ToString().Trim() + "  ", 2);
                dwVKPOS["VKPAUFLFD"] = m_Data.Right("000000" + txtVKPAUFLFD.Text.ToString(), 6);
                ReloadVVTLIEF(dwVKPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void dlgVVLIEF_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void txtVVLIEFSCH_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                return;
                string vvliefsch = m_Data.Right("00000000" + ((TextBox)sender).Text, 8);
                if (vvliefsch == "00000000")
                    return;
                LoadData(vvliefsch);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
           
        }
        private void LoadData(string vvliefsch)
        {
            string tbname = "";
            try
            {
                setUI.Clear();
                setUI.AcceptChanges();
                DataSet result = m_Data.BuildData(vvliefsch);
                foreach (DataTable tb in result.Tables)
                {
                    try
                    {
                        tbname = tb.TableName;
                        setUI.Merge(tb);
                    }
                    catch (Exception ex)
                    {
                        Tracer.Error(ex.StackTrace, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
